import { _decorator, Component, Node, Sprite, tween, Tween, UIOpacity, Vec3 } from 'cc';
import { GlobalData } from '../../../Data/GlobalData';
const { ccclass, property } = _decorator;

@ccclass('Bun')
/**
 * 包子
 */
export class Bun extends Component {

    //包子
    @property(Sprite)
    private BunSprite: Sprite = null;

    //筷子
    @property(Sprite)
    private Stick0: Sprite = null;

    //筷子
    @property(Sprite)
    private Stick1: Sprite = null;

    //筷子位置
    private StickPos0: Vec3 = new Vec3(15, 58, 0);
    private StickPos1: Vec3 = new Vec3(60, 51, 0);

    //包子索引
    public BunIndex: number = -1;

    /**
     * 我是在阴影中么
     */
    public IamInShadow: boolean = false;

    //透明度
    @property(UIOpacity)
    private Opacity: UIOpacity = null;

    /**
     * 更新
     */
    public UpdateBun(_bunindex: number) {
        this.BunIndex = _bunindex;
        this.BunSprite.spriteFrame = app.resManager.SpriteFrames[GlobalData.Bun_Spirte + _bunindex];
        this.BunSprite.node.setPosition(new Vec3(0, -52, 0))
        this.BunSprite.node.setScale(new Vec3(1, 1, 1));
        this.BunSprite.node.active = true;
        this.Opacity.opacity = 255;

        this.Stick0.node.active = false;
        this.Stick1.node.active = false;
        this.Stick0.spriteFrame = app.resManager.SpriteFrames[GlobalData.Stick_Sprite + this.BunIndex];
        this.Stick1.spriteFrame = app.resManager.SpriteFrames[GlobalData.Stick_Sprite + this.BunIndex];

        this.PlayAppearAnimation();

    }

    /**
     * 播放出现动画
     */
    public PlayAppearAnimation() {
        this.BunSprite.node.setScale(1, 0, 0);
        Tween.stopAllByTarget(this.BunSprite.node);
        tween(this.BunSprite.node).to(0.2 + 0.3 * Math.random(), { scale: new Vec3(1, 1, 1) }, { easing: 'backOut' }).start();
    }

    /**
     * 播放消失动画
     */
    public PlayDisappearAnimation(_pos0: Vec3, _pos1: Vec3, _cb: Function) {

        this.Stick0.node.active = true;
        this.Stick1.node.active = true;

        this.Stick0.node.setWorldPosition(_pos0);
        this.Stick1.node.setWorldPosition(_pos1);

        tween(this.Stick0.node).to(0.1, { scale: new Vec3(1.2, 1.2, 1.2) }).to(0.1, { scale: new Vec3(1, 1, 1) }).to(0.3, { position: this.StickPos0 }).to(0.4, { position: new Vec3(12, 158, 0) }).start();
        tween(this.Stick1.node).to(0.1, { scale: new Vec3(1.2, 1.2, 1.2) }).to(0.1, { scale: new Vec3(1, 1, 1) }).to(0.3, { position: this.StickPos1 }).to(0.4, { position: new Vec3(60, 151, 0) }).start();
        tween(this.Opacity).delay(0.2 + 0.3).to(0.4, { opacity: 100 }).start();
        tween(this.BunSprite.node).delay(0.2 + 0.3).to(0.4, { position: new Vec3(0, 58, 0) }).call(() => {
            this.BunIndex = -1;
            this.BunSprite.node.active = false;
            this.Stick0.node.active = false;
            this.Stick1.node.active = false;
            _cb && _cb();
        }).start();

    }

    /**
     * 直接消失
     */
    public IamDisappear(_cb: Function) {
        this.BunSprite.node.setScale(1, 1, 1);
        Tween.stopAllByTarget(this.BunSprite.node);
        tween(this.BunSprite.node).to(0.2 + 0.3 * Math.random(), { scale: new Vec3(1, 0, 0) }, { easing: 'backIn' }).call(() => {
            this.BunIndex = -1;
            this.BunSprite.node.active = false;
            _cb();
        }).start();

    }

    /**
     * 我是否显示阴影
     */
    public IamShowShadow(_isshow: boolean) {
        this.IamInShadow = _isshow;
        this.BunSprite.spriteFrame = app.resManager.SpriteFrames[(!_isshow ? GlobalData.Bun_Spirte : GlobalData.Bun_Shadow_Spirte) + this.BunIndex];
    }



}


